<?php
function GetCount ($numcount, $base_num, $multitype = 1)
{
    $c = 0;
    if ($multitype == 1)
    {
        if ($base_num == 5)
        {
            $c = $numcount * ( $numcount - 1 ) * ( $numcount - 2 ) * ( $numcount - 3 ) * ( $numcount - 4 ) / 120;
        }
        else
        {
            if ($base_num == 4)
            {
                $c = $numcount * ( $numcount - 1 ) * ( $numcount - 2 ) * ( $numcount - 3 ) / 24;
            }
            else
            {
                if ($base_num == 3)
                {
                    $c = $numcount * ( $numcount - 1 ) * ( $numcount - 2 ) / 6;
                }
                else
                {
                    if ($base_num == 2)
                    {
                        $c = $numcount * ( $numcount - 1 ) / 2;
                    }
                }
            }
        }
    }
    else
    {
        $c = $numcount - ( $base_num - 1 );
    }
    return $c;
}

function betin ($bettype, $subid, $num, $rate, $money, $stime, $amount2, $disk)
{
    global $curruser;
    global $lotto_id;
    global $prot_start;
    global $raters;
    $useAmt = Get_bet_amounts_Info($bettype, implode(",", $num), $subid);
    if ($amount2 < $money + $useAmt)
    {
        return false;
    }
    $payoff = round($money * ( $rate - 1 ));
    $numbers = $subid . "|" . implode(",", $num);
    $subid_count = intval(substr($subid, 0, 1));
    $fields = array();
    $fields['user_id'] = $curruser['id'];
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = $stime;
    $fields['bettype'] = $bettype;
    $fields['numbers'] = "'{$numbers}'";
    $fields['line_nums'] = "'" . implode(",", $num) . "'";
    $fields['amount'] = $money;
    $fields['unitmoney'] = $money;
    $fields['payoff'] = $payoff;
    $fields['disk'] = $disk;
    $rate = 110;
    $l = 0;
    while ($l < count($num))
    {
        $rate = $raters->f["r" . $num[$l]] < $rate ? $raters->f["r" . $num[$l]] : $rate;
        ++$l;
    }
    if (110 <= $rate || $rate <= 0)
    {
        account_err("注单存入失败, 赔率值错误！");
    }
    $fields['rate'] = $rate;
    $u2 = get_user($curruser['prev_id']);
    $u3 = get_user($u2['prev_id']);
    $u4 = get_user($u3['prev_id']);
    $u5 = get_user($u4['prev_id']);
    $fields['acl2'] = $u2['id'];
    $fields['acl3'] = $u3['id'];
    $fields['acl4'] = $u4['id'];
    $fields['acl5'] = $u5['id'];
    $a1 = GetBack($curruser['id'], $fields['bettype'] . $subid_count, $disk, 1);
    $a2 = GetBack($u2['id'], $fields['bettype'] . $subid_count, $disk, 2);
    $a3 = GetBack($u3['id'], $fields['bettype'] . $subid_count, $disk, 3);
    $a4 = GetBack($u4['id'], $fields['bettype'] . $subid_count, $disk, 4);
    $a5 = GetBack($u5['id'], $fields['bettype'] . $subid_count, $disk, 5);
    $fields['back1'] = $a1;
    $fields['back2'] = $a2;
    $fields['back3'] = $a3;
    $fields['back4'] = $a4;
    $fields['back5'] = $a5;
    $a6 = GetBack($u5['supe_account'], $fields['bettype'] . $subid_count, $disk, 255);
    $fields['back6'] = $a6;
    $fields['ip'] = "'" . getIP() . "'";
    $protmod = $u5['protmod'];
    $fields['mpercent'] = $u2['mpercent'];
    $fields['percent2'] = $u2['percent0'];
    $fields['percent3'] = min($u3['percent1'], $u4['mpercent'] - $fields['mpercent'] - $fields['percent2']);
    $fields['percent4'] = min($u4['percent1'], $u5['mpercent'] - $fields['mpercent'] - $fields['percent2'] - $fields['percent3']);
    $fields['upercent'] = 100 - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] - $fields['percent4'];
    if ($fields['percent3'] < 0 || $fields['percent4'] < 0 || $fields['upercent'] < 0)
    {
        err("占成参数设置错误，注单无法存档！");
    }
    $fields['r2'] = $money * $fields['percent2'] / 100;
    $fields['r3'] = $money * $fields['percent3'] / 100;
    $fields['r4'] = $money * $fields['percent4'] / 100;
    $fields['r6'] = $money * $fields['upercent'] / 100;
	$newid = db_insert("bet", $fields);
	
    if (!$newid)
    {
        return false;
    }
	$rs=getdb("select user_id from k_user_bet where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
	if(!$rs->eof)
		getdb("update k_user_bet set amount_6h=amount_6h+".$money." where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
	else
		getdb("insert into k_user_bet set amount_6h=amount_6h+".$money." ,user_id='".$curruser['id']."',lotto_id='$lotto_id'");
    if ($prot_start == 1 && $protmod == 1)
    {
        $tmpnum = implode(",", $num);
        auto_prots(2, $bettype, $tmpnum, $rate, $u2, $u3, $u4, $u5, $stime, $subid);
        auto_prots(3, $bettype, $tmpnum, $rate, $u2, $u3, $u4, $u5, $stime, $subid);
        auto_prots(4, $bettype, $tmpnum, $rate, $u2, $u3, $u4, $u5, $stime, $subid);
        auto_prots(5, $bettype, $tmpnum, $rate, $u2, $u3, $u4, $u5, $stime, $subid);
    }
    return true;
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$bettype = 36;
$ui = new UI();
$subid_Arr = array(21, 31, 41,51);
$subid = intval($_POST['subid']);
$subid_count = intval(substr($subid, 0, 1));
$subid_check = intval(substr($subid, 1, 1));
if (!in_array($subid, $subid_Arr))
{
    account_err("!错误的参数！");
}
if (!isset($_POST['num1']))
{
    account_err("错误的参数(num1)！");
}
$num1 = $_POST['num1'];
if ($num1 == "")
{
    account_err("请选择尾数！");
}
$num_r1 = explode(",", $num1);
$normal = !isset($_POST['num2']);
$num2 = "";
$num_r2 = array();
if (!$normal)
{
    $num2 = $_POST['num2'];
    if ($num1 == "")
    {
        account_err("错误的参数(num2)！");
    }
    $num_r2 = explode(",", $num2);
}
$betUnionNum = intval($_POST['betUnionNum']);
if ($betUnionNum < 1)
{
    account_err("错误的参数(betUnionNum)！");
}
$g_bignum = array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十");
$num_count1 = count($num_r1);
if ($normal)
{
    if ($num_count1 < $subid_count)
    {
        account_err("最少必须选择" . $g_bignum[$subid_count] . "种尾数！");
    }
    else
    {
        if (7 < $num_count1)
        {
            account_err("连碰最多只能选择" . $g_bignum[7] . "种尾数！");
        }
    }
}
else
{
    $num_count2 = count($num_r2);
    if ($num_count1 < $subid_count - 1)
    {
        account_err("胆拖最少必须选择" . $g_bignum[$subid_count - 1] . "种尾数为胆！");
    }
    else if ($num_count1 + $num_count2 < $subid_count)
    {
        account_err("胆拖最少必须选择" . $g_bignum[$subid_count] . "种尾数！");
    }
    else if (9 < $num_count2 && $normal == 0)
    {
        account_err("胆拖最多只能选择" . $g_bignum[9] . "种尾数！");
    }
}
$tpl = new Template2();
$tpl->load(array("main" => "proc18.htm"));
$tpl->set("account", $curruser['account']);
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("<p class=error>帐号已被暂停使用</p>");
    exit();
}
$rs = getdb("select * from lotto where open>='" . date("Y-m-d") . "' order by open");
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>目前没有可以下注期数！</p>");
    exit();
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$lotto_ns = $rs->f['n1'] + $rs->f['n2'] + $rs->f['n3'] + $rs->f['n4'] + $rs->f['n5'] + $rs->f['n6'];
$conf = new Config();
$prot_start = $conf->get("protect_start");
$stopbet = $conf->get("waitbet");
if (date("Y-m-d") < $open)
{
    exit();
}
else if (date("Y-m-d") == $open)
{
    if ($stopbet <= date("Hi") || 0 < $lotto_ns)
    {
        $ui->showErrorPage("<p>下注期数：" . d3($round) . "<br>开奖日：" . $open . "</p>" . "<p class=error>已经截止下注！</p>");
        exit();
    }
    else if (date("Hi") <= $conf->get("startbet"))
    {
        exit();
    }
}
$tpl->set("open", $open);
$tpl->set("round", d3($round));
$tpl->set("lotto_id", $lotto_id);
$tpl->set("account", $curruser['account']);
$tpl->set("amount", $curruser['amount']);
$chktime2 = 0;
$chk_rs = getdb("select createtime from bet where lotto_id=" . $lotto_id . " and user_id=" . $curruser['id'] . " and bettype=" . $bettype . " and rame=0");
while (!$chk_rs->eof)
{
    $chktime2 = $chk_rs->f[0];
    $chk_rs->movenext();
}
$chk_rs->close();
$chktime1 = systime();
if (intval($chktime1) - intval($chktime2) < 12)
{
    account_err("请於 " . ( 12 - ( intval($chktime1) - intval($chktime2) ) ) . "秒後才继续下注！");
}
$rs1 = getdb("select amount_6h from k_user_bet where user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id);
$usedamount = $rs1->f[0] ? $rs1->f[0] : 0;
$rs1->close();
$tpl->set("usedamount", $usedamount);
$betforms = "";
$num1 = implode(",", $num_r1);
$betforms .= "<input type=hidden name=\"num1\" value=\"" . $num1 . "\">\n";
if (!$normal)
{
    $num2 = implode(",", $num_r2);
    $betforms .= "<input type=hidden name=\"num2\" value=\"" . $num2 . "\">\n";
}
$tpl->set("betforms", $betforms);
$tpl->set("type", $subid);
$ratetype = 1;
$err = 0;
$j = 0;
while ($j < $num_count1 - 1)
{
    $cz = $num_r1[$j] . "";
    if ($cz == "")
    {
        ++$err;
    }
    $i = $j + 1;
    while ($i < $num_count1)
    {
        switch ($num_r1[$i])
        {
            case 0 :
            case 1 :
            case 2 :
            case 3 :
            case 4 :
            case 5 :
            case 6 :
            case 7 :
            case 8 :
            case 9 :
                if ($num_r1[$i] == $cz)
                {
                    ++$err;
                }
                break;
            default :
                ++$err;
                break;
        }
        ++$i;
    }
    ++$j;
}
if (!$normal)
{
    $betcounts = $betUnionNum;
    $j = 0;
    while ($j < $num_count2 - 1)
    {
        $cz = $num_r2[$j] . "";
        if ($cz == "")
        {
            ++$err;
        }
        $i = $j + 1;
        while ($i < $num_count2)
        {
            switch ($num_r2[$i])
            {
                case 0 :
                case 1 :
                case 2 :
                case 3 :
                case 4 :
                case 5 :
                case 6 :
                case 7 :
                case 8 :
                case 9 :
                    if ($num_r2[$i] == $cz)
                    {
                        ++$err;
                    }
                    break;
                default :
                    ++$err;
                    break;
            }
            ++$i;
        }
        ++$j;
    }
}
if (0 < $err)
{
    if ($normal)
    {
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "玩法(36)，使用不正常方式下注（" . implode(",", $num_r1) . "）");
        account_err("请勿以不正常方式下注，你的 IP 已经被记录并已经通知你的上层管理了！");
    }
    else
    {
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "玩法(36)，使用不正常方式下注（" . implode(",", $num_r1) . "：" . implode(",", $num_r2) . "）");
        account_err("请勿以不正常方式下注，你的 IP 已经被记录并已经通知你的上层管理了！");
    }
}
if ($normal)
{
    $betcounts = GetCount($num_count1, $subid_count, $normal);
    $betUnionNum = $betcounts;
    asort($num_r1);
    reset($num_r1);
}
$tpl->set("betname", "连尾");
$disk = getUserValidDisk($curruser);
$disk1 = $g_disk_code[$disk];
echo "<input type=hidden name=disk value=" . strpos(" abcd", $disk1) . ">";
$tpl->set("diskStr", strtoupper($disk1) . "盘");
$raters = getdb("select * from rate36 where lotto_id={$lotto_id} and disk={$disk} and subid={$subid}");
if ($raters->eof)
{
    account_err("系统目前维护中，请稍後！");
}
$maxRate = 0;
$minRate = 999;
$l = 0;
while ($l < count($num_r1))
{
    $minRate = $raters->f["r" . $num_r1[$l]] < $minRate ? $raters->f["r" . $num_r1[$l]] : $minRate;
    $maxRate = $maxRate < $raters->f["r" . $num_r1[$l]] ? $raters->f["r" . $num_r1[$l]] : $maxRate;
    if ($minRate == 0 || $minRate == 999)
    {
        account_err("连尾中的 " . $g_bignum[$subid_count] . "尾 没有开放下注！");
    }
    ++$l;
}
$ratechange = false;
$betcontent = "<b>" . $g_bignum[$subid_count] . "尾" . ( $subid_check == 1 ? "中" : "不中" ) . "</b><br>";
$betcontent .= "<font color=red>最高 @ <font color=red id=maxrate>" . $maxRate . "</font><br>";
$betcontent .= "<font color=red>最低 @ <font color=red id=minrate>" . $minRate . "</font>";
if ($normal)
{
    $betcontent .= "<br>连碰 [ " . $num1 . " ]<br>合计<font color=red><b>" . $betUnionNum . "</b></font>组，存成<font color=red><b>" . $betUnionNum . "</b></font>笔";
}
else
{
    $betcontent .= "<br>胆拖 [ " . $num1 . " ] > [ " . $num2 . " ]<br>合计<font color=red><b>" . $betUnionNum . "</b></font>组，存成<font color=red><b>" . $betUnionNum . "</b></font>笔";
}
$tpl->set("betcontent", $betcontent);
$tpl->set("rates", trimfloat($minRate, 1));
$tpl->set("betUnionNum", $betUnionNum);
$betmin = $conf->get("betmin");
$tpl->set("min", $betmin);
$betmax = $conf->get("betmax");
$tpl->set("max", $betmax);
$tpl->set("max_str", $betmax);
$rs1 = getdb("select * from amounts where bettype={$bettype} and user_id=" . $curruser['id'] . " and subid={$subid_count}");
$amount1 = $rs1->f['amount1'] - 0;
$amount2 = $rs1->f['amount2'] - 0;
$rs1 = getdb("select sum(amount) as amount,numbers,line_nums from bet where bettype=" . $bettype . " and user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id . " and rame=0 group by numbers");
                                while (!$rs1->eof)
                                {
                                   if ($rs1->f['line_nums'] == $num1)
                                    {
                                        $bet += $rs1->f['amount'];//已下注每期总额
                                    }
                                    
                                    $rs1->movenext();
                                }
	$m = $amount2-$bet;
$tpl->set("amount1", $amount1);
$tpl->set("amount2", $amount2);
$tpl->set("amount1_str", $amount1);
$tpl->set("amount2_str", $m);
$usemaxamount = $curruser['usemaxamount'];
if (isset($_POST['money']) && $ratechange == false)
{
    $money = intval($_POST['money']);
    if ($money == 0)
    {
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "使用外挂攻击");
        account_err("不要随便攻击本站！你的 IP 已经被记录了！");
    }
    if ($money < $betmin)
    {
        account_err("下注金额不能小於最低限额！");
    }
    if ($amount1 < $money)
    {
        account_err("下注金额不能超过单注限额！");
    }
    if ($curruser['balance'] < $money * $betUnionNum)
    {
        account_err("总下注金额不能超过信用额度！");
    }
    if ($m < $money * $betUnionNum)
    {
        account_err("总下注金额不能超过本期限额！");
    }
    if ($conf->get("waitbet") <= date("Hi"))
    {
        account_err("已经截止下注！");
    }
    $stime = systime();
    if ($normal)
    {
        if ($subid_count == 5)
        {
            $i = 0;
            while ($i < $num_count1 - 4)
            {
                $j = $i + 1;
                while ($j < $num_count1 - 3)
                {
                    $k = $j + 1;
                    while ($k < $num_count1 - 2)
                    {
                        $l = $k + 1;
                        while ($l < $num_count1 - 1)
                        {
                            $m = $l + 1;
                            while ($m < $num_count1)
                            {
                                $nums = explode(",", $num_r1[$i] . "," . $num_r1[$j] . "," . $num_r1[$k] . "," . $num_r1[$l] . "," . $num_r1[$m]);
                                sort($nums);
                                reset($nums);
                                $rate = 0;
                                if (!betin($bettype, $subid, $nums, $rate, $money, $stime, $amount2, $disk))
                                {
                                    account_err("号码:" . implode(",", $nums) . "超过额度！");
                                }
                                ++$m;
                            }
                            ++$l;
                        }
                        ++$k;
                    }
                    ++$j;
                }
                ++$i;
            }
            //break;
        }
        else
        {
            if ($subid_count == 4)
            {
                $i = 0;
                while ($i <= $num_count1 - 3)
                {
                    $j = $i + 1;
                    while ($j <= $num_count1 - 2)
                    {
                        $k = $j + 1;
                        while ($k < $num_count1 - 1)
                        {
                            $l = $k + 1;
                            while ($l < $num_count1)
                            {
                                $nums = explode(",", $num_r1[$i] . "," . $num_r1[$j] . "," . $num_r1[$k] . "," . $num_r1[$l]);
                                sort($nums);
                                reset($nums);
                                $rate = 0;
                                if (!betin($bettype, $subid, $nums, $rate, $money, $stime, $amount2, $disk))
                                {
                                    account_err("号码:" . implode(",", $nums) . "超过额度！");
                                }
                                ++$l;
                            }
                            ++$k;
                        }
                        ++$j;
                    }
                    ++$i;
                }
               //break;
            }
            else
            {
                if ($subid_count == 3)
                {
                    $i = 0;
                    while ($i <= $num_count1 - 2)
                    {
                        $j = $i + 1;
                        while ($j <= $num_count1 - 2)
                        {
                            $k = $j + 1;
                            while ($k < $num_count1)
                            {
                                $nums = explode(",", $num_r1[$i] . "," . $num_r1[$j] . "," . $num_r1[$k]);
                                sort($nums);
                                reset($nums);
                                $rate = 0;
                                if (!betin($bettype, $subid, $nums, $rate, $money, $stime, $amount2, $disk))
                                {
                                    account_err("号码:" . implode(",", $nums) . "超过额度！");
                                }
                                ++$k;
                            }
                            ++$j;
                        }
                        ++$i;
                    }
                    //break;
                }
                else
                {
                    if ($subid_count == 2)
                    {
                        $i = 0;
                        while ($i <= $num_count1 - 2)
                        {
                            $j = $i + 1;
                            while ($j < $num_count1)
                            {
                                $nums = explode(",", $num_r1[$i] . "," . $num_r1[$j]);
                                sort($nums);
                                reset($nums);
                                $rate = 0;
                                if (!betin($bettype, $subid, $nums, $rate, $money, $stime, $amount2, $disk))
                                {
                                    account_err("号码:" . implode(",", $nums) . "超过额度！");
                                }
                                ++$j;
                            }
                            ++$i;
                        }
                    }
                }
            }
        }
    }
    else if ($subid_count == 5)
    {
        $i = 0;
        while ($i < $num_count2)
        {
            $nums = explode(",", $num_r1[0] . "," . $num_r1[1] . "," . $num_r1[2] . "," . $num_r1[3] . "," . $num_r2[$i]);
            sort($nums);
            reset($nums);
            $rate = 0;
            if (!betin($bettype, $subid, $nums, $rate, $money, $stime, $amount2, $disk))
            {
                account_err("号码:" . implode(",", $nums) . "超过额度！");
            }
            ++$i;
        }
    }
    else if ($subid_count == 4)
    {
        $i = 0;
        while ($i < $num_count2)
        {
            $nums = explode(",", $num_r1[0] . "," . $num_r1[1] . "," . $num_r1[2] . "," . $num_r2[$i]);
            sort($nums);
            reset($nums);
            $rate = 0;
            if (!betin($bettype, $subid, $nums, $rate, $money, $stime, $amount2, $disk))
            {
                account_err("号码:" . implode(",", $nums) . "超过额度！");
            }
            ++$i;
        }
    }
    else if ($subid_count == 3)
    {
        $i = 0;
        while ($i < $num_count2)
        {
            $nums = explode(",", $num_r1[0] . "," . $num_r1[1] . "," . $num_r2[$i]);
            sort($nums);
            reset($nums);
            $rate = 0;
            if (!betin($bettype, $subid, $nums, $rate, $money, $stime, $amount2, $disk))
            {
                account_err("号码:" . implode(",", $nums) . "超过额度！");
            }
            ++$i;
        }
    }
    else if ($subid_count == 2)
    {
        $i = 0;
        while ($i < $num_count2)
        {
            $nums = explode(",", $num_r1[0] . "," . $num_r2[$i]);
            sort($nums);
            reset($nums);
            $rate = 0;
            if (!betin($bettype, $subid, $nums, $rate, $money, $stime, $amount2, $disk))
            {
                account_err("号码:" . implode(",", $nums) . "超过额度！");
            }
            ++$i;
        }
    }
	
    $payoff = round($money * ( $maxRate - 1 ));
    $tpl->load("succ", "succ.htm");
    $tpl->set("ordertype", $betcontent);
    $tpl->set("money", $money);
    $tpl->set("payoff", $payoff);
    $tpl->set("myscript", "parent.mainFrame.location.reload();");
    $tpl->show("succ");
}
else
{
    if ($ratechange == true)
    {
        if (isset($_POST['money']))
        {
            $money = intval($_POST['money']);
            $tpl->set("money", $money);
        }
        $tpl->set("message", "<tr><td colspan=2 bgcolor=red align=center><font color=white>赔率已经变动！</font></td></tr>");
    }
    $tpl->show("main");
}
?>